package easymall.util;
import java.util.Date;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//发送一封邮件的帮助类
public class SendEmailUtil {
	
	private static String host = "http://localhost:8080"; //localhost/Easymall
	private static String sendEmailAccount = "1614545312@qq.com";
    private static String sendEmailPwd = "cmdaenjpaujnfcfi";
    
    private static String content = "<h3 style=\"color: red;\">EasyMall团队欢迎您注册~</h3>\n" + 
    		"<p>请<a href=\""+host+"/user/motifyvaild?id=";
    private static int id;
    private static String content_2 =  "\" style=\"color: blue;\">点击此处</a>验证您的邮箱！</p>\n" + 
    		"<p style=\"color: #aaa; font-size: 12px;\">如果不是您的操作，请忽略此消息</p>"	; 
    private static String emailProtocolType = "smtp"; 
    private static String sendEmailSMTPHost = "smtp.qq.com";
	
    //发送一封邮件
    //receive 收件人邮箱地址
    public static void sendEmail(String receiveMailAccount,int id) throws Exception {
    	
    	Session session = initSession();
    	
    	// 创建一封邮件
        MimeMessage message = createMimeMessage(session, sendEmailAccount, receiveMailAccount,id);

        // 根据 Session 获取邮件传输对象
        Transport transport = session.getTransport();
        
        //获取传输对象
        transport.connect(sendEmailAccount, sendEmailPwd);

        // 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
        transport.sendMessage(message, message.getAllRecipients());

        // 关闭连接
        transport.close();
    }
    
	
	/**
     * 创建一封只包含文本的简单邮件
     *
     * @param session 和服务器交互的会话
     * @param sendMail 发件人邮箱
     * @param receiveMail 收件人邮箱
     * @return
     * @throws Exception
     */
    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail,int id) throws Exception {
        // 1. 创建一封邮件
        MimeMessage message = new MimeMessage(session);

        // 2. From: 发件人
        message.setFrom(new InternetAddress(sendMail, "EasyMall", "UTF-8"));

        // 3. To: 收件人
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用户", "UTF-8"));
        
        // 4. Subject: 邮件主题
        message.setSubject("EasyMall注册验证", "UTF-8");

        // 5. Content: 邮件正文（可以使用html标签）
        message.setContent(content+id+content_2, "text/html;charset=utf-8");

        // 6. 设置发件时间
        message.setSentDate(new Date());

        // 7. 保存设置
        message.saveChanges();

        return message;
    }
    
    //初始化session会话对象
    private static Session initSession() {
    	// 1. 创建参数配置, 用于连接邮件服务器的参数配置
        Properties props = new Properties();                    // 参数配置
        props.setProperty("mail.transport.protocol", emailProtocolType);   // 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host", sendEmailSMTPHost);   // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true");            // 需要请求认证
        
        final String smtpPort = "465";
        props.setProperty("mail.smtp.port", smtpPort);
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.ssl.enable", "true");
        props.setProperty("mail.smtp.socketFactory.port", smtpPort);
        // 2. 根据配置创建会话对象, 用于和邮件服务器交互
        return Session.getInstance(props);
    }
}
